package com.bite.mybatis.mapper;

import com.bite.mybatis.model.UserInfo;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Mapper
public interface UserInfoMapper {


    @ResultMap("BaseMap")
    @Select("select * from userinfo")
    List<UserInfo> queryUserInfos();


    @Select("select * from userinfo where id = ${value}")
    UserInfo queryUserInfo(Integer id);


    @Select("select * from userinfo where username = '${username}'")
    UserInfo queryUserInfoByName(String username);

    @Select("select * from userinfo where id = #{id} and username= ${name}")
    UserInfo queryUserInfoByIdAndName(Integer id, String name);


    @Select("select * from userinfo where id = #{param1} and username= #{param2}")
    UserInfo queryUserInfoByIdAndName2(@Param("id") Integer id, @Param("name") String name);

    @Insert("insert into userinfo (username, `password`, age, gender, phone, delete_flag)" +
            " values(#{username},#{password},#{age},#{gender},#{phone}, #{deleteFlag})")
    Integer insert(UserInfo userInfo);


    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username, `password`, age, gender, phone, delete_flag)" +
            " values(#{userInfo.username},#{userInfo.password}," +
            "#{userInfo.age},#{userInfo.gender},#{userInfo.phone}, #{userInfo.deleteFlag})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);


    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

    @Update("update userinfo set `password` = #{password} where id = #{id}")
    void update(UserInfo userInfo);

    /**
     * 起别名
     * @return
     */
    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime" +
            "  from userinfo")
    List<UserInfo> queryUserInfos2();

    /**
     * 注解
     * @return
     */
    @Results(id="BaseMap", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
    })
    @Select("select *  from userinfo")
    List<UserInfo> queryUserInfos3();



    @Select("select * from userinfo")
    List<UserInfo> queryUserInfos4();

    //演示SQL注入
    @Select("select * from userinfo where username= #{username} and password = #{password}")
    List<UserInfo> queryUserByNameAndPassword(String username, String password);


    @Select("select * from userinfo where username= '${username}' and password = '${password}'")
    List<UserInfo> queryUserByNameAndPassword2(String username, String password);

    //排序
    @Select("select * from userinfo order by id ${order}")
    List<UserInfo> queryUserInfoByOrder(String order);

    @Select("select * from userinfo where username like CONCAT('%',#{name},'%')")
    List<UserInfo> queryUserInfoByLike(String name);

    @Insert("<script>insert into userinfo (username, password," +
            "<if test='age!=null'> age,</if> " +
            "gender, phone) " +
            "values (#{username}, #{password}, " +
            "<if test='age!=null'> #{age},</if> " +
            "#{gender}, #{phone}) " +
            "</script>")
    Integer insertByCondition(UserInfo userInfo);
}
